package com.tcl.utilcode.managers;

import com.tcl.utilcode.util.LogUtils;
import com.tcl.utilcode.util.SDCardUtils;
import com.tcl.utilcode.util.StringUtils;

import java.io.File;
import java.io.FileInputStream;
import java.util.List;

/**
 * @Author Mack
 * @Date 2020/10/22
 */
public class ExternalConfigManager {

    private static final String CONFIG_NAME = "EvaExtCfg.txt";
    private ExternalConfigItem mConfigItem = new ExternalConfigItem();

    private static class ExternalConfigItem {
        private static final String LOG_INFO_NAME = "evaLogInfo";
        private boolean logInfo;

        private static final String LOG_SAVE_DAYS = "evaLogSaveDays";
        private int logSaveDays = -1;

        private static final String SHOW_PREVIEW_ENABLE = "evaShowPreviewEnable";
        private boolean showPreviewEnable;

        private static final String FPS = "evaFps";
        private int fps = -1;
    }

    private ExternalConfigManager() {}

    private static class SingletonHolder {
        private static final ExternalConfigManager INSTANCE = new ExternalConfigManager();
    }

    public static final ExternalConfigManager getInstance() {
        return SingletonHolder.INSTANCE;
    }


    public void loadConfig() {
        try {
            List<String> sdCardPaths = SDCardUtils.getMountedSDCardPath();
//            LogUtils.d("sdCardPaths:" + sdCardPaths);
            int size = sdCardPaths.size();
//            if (size > 0) {
                mConfigItem = new ExternalConfigItem();
//                File file = findAvailableFile(sdCardPaths);
                File file = new File("/sdcard" + File.separator + CONFIG_NAME);

                if (file != null && file.exists() && file.length() > 0) {
                    int len = (int)file.length();
                    byte[] fileContent = new byte[len];
                    FileInputStream fileOutputStream = new FileInputStream(file);

                    try {
                        fileOutputStream.read(fileContent);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                    String fileString = new String(fileContent);
                    String[] strings = fileString.split("\n");
                    for (String temp: strings) {
                        temp = temp.trim();
                        if (temp.equalsIgnoreCase("#") || temp.equalsIgnoreCase("//")) {
                            continue;
                        }

                        if (temp.length() >= 2) {
                            String[] subStrings = temp.split("=");

                            if (subStrings.length >= 2) {
                                String name = subStrings[0].trim();
                                String value = subStrings[1].trim();
                                String[] vs = value.split(" |//|;");
                                if (vs.length > 0) {
                                    value = vs[0].trim();
                                }

                                decodeValue(name, value);
                            }
                        }
                    }
//                    execTask();

                    file = null;
                    fileOutputStream = null;
                    fileString = null;
                    fileContent = null;
                }
//            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private File findAvailableFile(List<String> sdCardPaths){
        File file = null;
        int size = sdCardPaths.size();
        for (int i = size - 1; i >= 0; i--) {
            String sdPath = sdCardPaths.get(i);
            sdPath = sdPath + File.separator + CONFIG_NAME;
            file = new File(sdPath);
            if (file.exists() && file.length() > 0) {
                break;
            }
        }
        return file;
    }

    public String getSDCardEvaPath(){
        List<String> sdCardPaths = SDCardUtils.getMountedSDCardPath();
        int size = sdCardPaths.size();
        File file = null;
        for (int i = size - 1; i >= 0; i--) {
            String sdPath = sdCardPaths.get(i);
            file = new File(sdPath);
            if (file.exists() && file.length() > 0) {
                break;
            }
        }

        String path = "";
        if(file != null && file.exists() && file.length() > 0){
            path = file.getPath() + File.separator + "eva";
        }

        return path;
    }

    private void decodeValue(String name, String value) {
        if (null != name && null != value) {
            if (name.equalsIgnoreCase(ExternalConfigItem.LOG_INFO_NAME)) {
                mConfigItem.logInfo = getBooleanValue(value);
            }else if(name.equalsIgnoreCase(ExternalConfigItem.LOG_SAVE_DAYS)){
                mConfigItem.logSaveDays = getIntValue(value);
            }else if(name.equalsIgnoreCase(ExternalConfigItem.SHOW_PREVIEW_ENABLE)){
                mConfigItem.showPreviewEnable = getBooleanValue(value);
            }else if(name.equalsIgnoreCase(ExternalConfigItem.FPS)){
                mConfigItem.fps = getIntValue(value);
            }
        }
    }

    private boolean getBooleanValue(String value) {
        if (null != value) {
            return value.equalsIgnoreCase("true") || value.equalsIgnoreCase("1");
        }

        return false;
    }

    private int getIntValue(String value) {
        if (null != value && value.length() > 0) {
            return StringUtils.toInt(value);
        }
        return 0;
    }

    public boolean isLogInfo() {
        if (null != mConfigItem) {
            return mConfigItem.logInfo;
        }
        return false;
    }

    public int getLogSaveDays(){
        if (null != mConfigItem) {
            return mConfigItem.logSaveDays;
        }
        return -1;
    }

    public boolean showPreviewEnable(){
        if (null != mConfigItem) {
            return mConfigItem.showPreviewEnable;
        }
        return false;
    }

    public int getFps(){
        if (null != mConfigItem) {
            return mConfigItem.fps;
        }
        return -1;
    }
}
